var Location = function (attributes) {
    this.fullname = attributes.fullname;
    this.countryAbbreviation = attributes.countryAbbreviation;
    this.taxUnitAbbreviation = attributes.taxUnitAbbreviation || attributes.countryAbbreviation;
    this.id = attributes.id;
    this.abbreviation = this.countryIsTaxUnit() ? this.countryAbbreviation : this.countryAbbreviation + " " + this.taxUnitAbbreviation;
};

Location.prototype = {
    countryIsTaxUnit: function() {
        return this.countryAbbreviation == this.taxUnitAbbreviation;
    },

    equals: function(other) {
        return !!other && other.id === this.id;
    }
};

// HIDE 4 COUNTRIES
Location.prototype.isRedundantLocation = function() {
    var redundantLocationAbbrs = ["CN", "CH", "CA", "US"];
    return redundantLocationAbbrs.contains(this.abbreviation);
};

Location.isValidLocationFullName = function(locationFullName) {
    return Location.isValidLocation(pwc.locationCache.getByFullName(locationFullName));
};

Location.isValidLocation = function(location) {
    return !!location && !location.isRedundantLocation();
};

///TODO: move to repository
var pwc = pwc || {};
pwc.locationCache = {
    getByLid: function(lid) {
        if (!this._cacheByLid) {
            this._cache();
        }
        return this._cacheByLid[lid] || null;
    },

    getByFullName: function(fullName) {
        if (!this._cacheByFullName) {
            this._cache();
        }
        return this._cacheByFullName[fullName] || null;
    },

    getAllFullNames: function() {
        if (!this._allFullNames) {
            this._cache();
        }
        return this._allFullNames;
    },

    getAllValidFullNames: function() {
        if (!this._allValidFullNames) {
            this._cache();
        }
        return this._allValidFullNames;
    },

    _cache: function() {
        var jsonDtos = this._requestAllLocations();
        this.resetCache(jsonDtos);
    },

    resetCache: function(jsonDtos) {
        this._cacheByLid = {};
        this._cacheByFullName = {};
        this._allFullNames = [];
        this._allValidFullNames = [];
        var me = this;
        jsonDtos.foreach(function(dto) {
            var location = new Location({
                id: dto.Id,
                fullname: dto.FN,
                countryAbbreviation: dto.CAb,
                taxUnitAbbreviation: dto.TAb
            });
            me._cacheByLid[location.id] = location;
            me._cacheByFullName[location.fullname] = location;
            me._allFullNames.push(location.fullname);

            //HIDE 4 COUNTRIES: cache the locations without redundant ones
            if (!location.isRedundantLocation()) {
                me._allValidFullNames.push(location.fullname);
            }
        });
    },

    _requestAllLocations: function() {
        var locationString = null;
        $.ajax({
            url: pwc.path.getUrl("location/locations"),
            async: false,
            type: "POST",
            data: {},
            cache: false,
            success: function(response) {
                locationString = response;
            }
        });
        return JSON.parse(locationString)
    }
};

